package com.polaris.live.dialog

import android.content.Context
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.BuildConfig
import com.polaris.live.common.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.databinding.DialogUpgradeBinding
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.resp.AppVersionVo
import com.polaris.live.common.util.SystemUtils

/**
 * 升级弹窗
 *
 * @author Created by 半仙 on 2023/8/9/009 11:12
 */
class UpgradeDialog(
    context: Context,
    private val version: AppVersionVo? = null,
    private val mainVersion: AppVersionVo? = null
) :
    CenterPopupView(context) {

    private lateinit var mBinding: DialogUpgradeBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_upgrade
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogUpgradeBinding.bind(popupImplView)
        version?.let {
            if (it.promptOnlyOnce) {
                CacheUtil.setUpdateVersion(it.buildVersion.toString(), true)
            }
        }
        mainVersion?.let {
            if (it.promptOnlyOnce) {
                CacheUtil.setMainUpdateVersion(it.buildVersion.toString(), true)
            }
        }
        val mVersion = version ?: mainVersion
        mVersion?.let {
            mBinding.tvMessage.text = it.content
            val layoutParams = mBinding.ivUpgrade.layoutParams as ConstraintLayout.LayoutParams
            if (it.forceUpdate) {
                mBinding.tvNext.visibility = View.GONE
                layoutParams.dimensionRatio = "885:615"
                mBinding.ivUpgrade.layoutParams = layoutParams
                mBinding.ivUpgrade.setImageResource(R.drawable.bg_upgrade_force)
            } else {
                layoutParams.dimensionRatio = "885:483"
                mBinding.ivUpgrade.layoutParams = layoutParams
                mBinding.ivUpgrade.setImageResource(R.drawable.bg_upgrade)
            }
        }

        mBinding.tvConfirm.setOnSingleClickListener {
            SystemUtils.launchAppDetail(BuildConfig.APPLICATION_ID)
        }

        mBinding.tvNext.setOnSingleClickListener {
            dismiss()
        }
    }
}